*This program calculates correlations between expected consumption risk and various household traits.
*Estimates are derived from univariate robust regressions using household-level data.
*The program is run for the project on consumption uncertainty and precautionary saving using DNB data
*November 2018 - Dimitris Christelis, Dimitris Georgarakos, Tullio Jappelli, Maarten van Rooij

*set trace on
version 12.1
clear all
macro drop _all
set maxvar 20000
set more off
set seed 123456

*ATTENTION - ATTENTION - ATTENTION 
*The following lines define the file path. Please adapt to own computer
do "c:/foldp/foldp.do"
global projf "${foldp}/DNB_survey_consumption/ConGrowth/Replication"

*Local of distributions of expectations (split triangular here)
local dist sptri 

*Local of measures of uncertainty about consumption growth
local ylist w005_hsd`dist'_consgr w005_hm`dist'_sqcgr
local ylist_n: word count `ylist'

local clustv nohhold

global bsn = 1000

*Reading the data
use "${projf}/Data/Data_cons_uncertainty.dta", clear

*Using only the observations that appear in the OLS and robust regressions
keep if hpconch_nyr!=. & hm`dist'_sqcgr!=. & regio!=.

forvalues m=1/`ylist_n' {

   global yvar: word `m' of `ylist'

   qui su ${yvar} [aw=wgth] 
   sca ymn= r(mean)
   sca ysd= r(sd)
   sca ynob= r(N)

   *Global of variables to which the measures of consumption uncertainty are correlated
   global xvar w005_hsd`dist'_incgr age hhslfemp hhretire hhunion hhsize
   global numxv: word count ${yvar} ${xvar}

   *******************************
   matrix a_`m' = J(${numxv},8,.)
   mat rownames a_`m' = ${yvar} ${xvar}
   mat colnames a_`m' = coeff_${yvar} se_${yvar} zstat_${yvar} pval_${yvar} N_${yvar} mean_${yvar} stdev_${yvar} blank

   mat a_`m'[1,1] = 1
   mat a_`m'[1,5] = ynob
   mat a_`m'[1,6] = ymn
   mat a_`m'[1,7] = ysd

   *****

   matrix o = J(${numxv},7,.)
   mat rown o = ${yvar} ${xvar}
   mat coln o = coeff se zstat pval N mean stdev

   mat o[1,1] = 1
   mat o[1,5] = ynob
   mat o[1,6] = ymn
   mat o[1,7] = ysd
   ********************************

   local k = 1

   foreach x of global xvar {

      local k = `k'+1

      **** Robreg ****
      set seed 1234567`k'
      qui bootstrap, cluster(`clustv') reps(${bsn}): robreg m ${yvar} `x', r
      assert e(converged) == 1
      sca nobs = e(N)

      mat a_`m'[`k',1] = _b[`x']
      mat a_`m'[`k',2] = _se[`x']
      mat a_`m'[`k',3] = (_b[`x'])/(_se[`x'])
      mat a_`m'[`k',4] = 2*ttail(nobs - 1, abs(a_`m'[`k',3]))
      mat a_`m'[`k',5] = nobs

      qui su `x' if e(sample)==1 [aw=wgth]
      sca xmn= r(mean)
      sca xsd= r(sd)
      mat a_`m'[`k',6] = xmn
      mat a_`m'[`k',7] = xsd


   *End of loop over covariates below
   } 

   if `m'==1 {
   
      mat afinal = a_`m'
   }

   else if `m'>1 {
   
      mat afinal = (afinal, a_`m')
   }

*End of loop over dependent variables
}

mat2txt, matrix(afinal) saving("${projf}/Results/corr_var/correl_robreg_b${bsn}_`dist'.txt") ///
   title(Correlations from robust regressions) replace format(%20.8f)

cap log close



